//
//  AuctionRecommendCollectionViewCell.m
//  MyNewProject
//
//  Created by 王春辉 on 2019/12/25.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "AuctionRecommendCollectionViewCell.h"
#import "AuctionRecommendCell.h"

#pragma mark -- mark 数据
#import "AuctionHomeModel.h"

@interface AuctionRecommendCollectionViewCell() {
    NSArray <AuctionRecommendCell *>*_cells;
}
@property (nonatomic, strong) AuctionRecommendCell *cell1;
@property (nonatomic, strong) AuctionRecommendCell *cell2;
@property (nonatomic, strong) AuctionRecommendCell *cell3;
@end
@implementation AuctionRecommendCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initContentViews];
    }
    return self;
}

/**
 初始化三个cell 用就用 不用就隐藏 😁
 */
- (void)initContentViews {
    _cells = @[self.cell1, self.cell2, self.cell3];
    [self.cell1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.top.mas_offset(10);
        make.bottom.mas_offset(-30);
        make.width.mas_equalTo(self.cell2);
    }];
    
    [self.cell2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.cell1.mas_right).mas_offset(5);
        make.top.bottom.mas_equalTo(self.cell1);
        make.width.mas_equalTo(self.cell3);
    }];
    
    [self.cell3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.cell2.mas_right).mas_offset(5);
        make.top.bottom.mas_equalTo(self.cell1);
        make.right.mas_offset(0);
    }];
}
#pragma mark -- mark 懒加载

- (AuctionRecommendCell *)cell1 {
    if (!_cell1) {
        self.cell1 = [[AuctionRecommendCell alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        _cell1.tag = 0;
        [_cell1 addGestureRecognizer:tap];
        [_cell1 prepareReload];
        [self addSubview:_cell1];
    }
    return _cell1;
}
- (AuctionRecommendCell *)cell2 {
    if (!_cell2) {
        self.cell2 = [[AuctionRecommendCell alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        _cell2.tag = 1;
        [_cell2 addGestureRecognizer:tap];
        [_cell2 prepareReload];
        [self addSubview:_cell2];
    }
    return _cell2;
}
- (AuctionRecommendCell *)cell3 {
    if (!_cell3) {
        self.cell3 = [[AuctionRecommendCell alloc] init];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        _cell3.tag = 2;
        [_cell3 addGestureRecognizer:tap];
        [_cell3 prepareReload];
        [self addSubview:_cell3];
    }
    return _cell3;
}

#pragma mark -- mark EVENT
- (void)tapGesture:(UITapGestureRecognizer *)sender {
    NSInteger index = sender.view.tag;
    if (self.cellTapGestureBlock) {
        self.cellTapGestureBlock(index);
    }
}

#pragma mark -- mark 业务逻辑
- (void)setDatas:(NSArray<AuctionHomeShopModel *> *)datas {
    if (_datas == datas || datas.count > _cells.count) {
        return; }
    _datas = datas;
    [self reloadDatas];
}
- (void)reloadDatas {
    for (int i = 0; i < _cells.count; i++) {
        AuctionRecommendCell *cell = _cells[i];
        [cell prepareReload];
    }
    for (int i = 0; i < self.datas.count; i++) {
        AuctionRecommendCell *cell = _cells[i];
        cell.hidden = false;
        AuctionHomeShopModel *model = self.datas[i];
        cell.des = model.describe;
        cell.title = model.name;
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:SKIMAGE_NAMED(@"cell_default_img")];
    }
}
@end
